<?php

namespace Platform\Services\Admin;

use Platform\Services\BaseService;

class AdminUserService extends BaseService {

    /**
     * 获取管理员用户信息
     * @param type $phone
     * @return type
     */
    public function getAdminUserByUsername($username) {
        $adminUserModel = model("Admin/AdminUser");
        $adminUser = $adminUserModel->getAdminUserByUsername($username);
        return $adminUser;
    }

    /**
     * 获取所有管理员信息
     */
    public function getAdminUserAll(){
        $adminUsers = model("Admin/AdminUser")->getAdminUserAll();
        return $adminUsers ;
    }

    /**
     * 用户登录
     * @param unknown_type $phone
     * @param unknown_type $password
     */
    function login($username, $password) {
        $adminUserModel = model("Admin/AdminUser");
        $adminUser = $this->getAdminUserByUsername($username);
        if (empty($adminUser)) {
            E("账号不存在");
        }
        if ($adminUser['password'] != md5($password)) {
            E("密码错误");
        }
        $adminUserModel->updateAdminUserLoginInfo($adminUser['adminId']);
        unset($adminUser['password']);
        return $adminUser;
    }

    /**
     * 添加管理用户
     * @param $username
     * @param $password
     * @param $roleId
     * @param $realName
     */
    function addAdminUser($username, $password, $roleId, $realName,$img,$adminId){
        $adminUserModel = model("Admin/AdminUser");
        $adminUserModel->addAdminUser($username, $password, $roleId, $realName,$img,$adminId);
    }

    /**
     * 搜索功能
     * @param $userId
     * @param $remark
     * @param $realName
     */
    function search($adminId,$remark,$realName){
        //获取角色信息

        $role = model("Admin/AdminRole")->getRoleByRemark($remark);

        $searchResult = model("Admin/AdminUser")->search($adminId,$role->id,$realName);

        return $searchResult;
    }


    /**
     * 通过adminId获取用管理员信息
     * @param $adminId
     */
    public function getAdminUserById($adminId){
        $adminUserModel = model("Admin/AdminUser");
        $adminUser = $adminUserModel->getAdminUserById($adminId);
        return $adminUser;
    }

    /**
     * 通过adminId修改用户信息
     * @param $adminId
     */
    public function updateUserInfoById($adminId,$password,$newPassword,$img){
        $adminUserModel = model("Admin/AdminUser");

        //密码不为空时则修改密码
        if(!empty(trim($password)) && !empty(trim($newPassword))){
            $adminUser = $adminUserModel->getAdminUserById($adminId);
            if($adminUser['password'] == $password){
                $adminUserModel->updateUserInfo($adminId,$newPassword);
            }else{
                E("原始密码不相同，修改失败");
            }
        }

        //头像不为空时则修改头像
        if(!empty($img)){
            $adminUserModel->updateImgById($adminId,$img);
        }

    }


}

?>
